package com.simtop.util;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.springframework.stereotype.Component;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

@Component
public class ExcelUtils {
    /**
     * @param is   导入文件输入流
     * @param clazz Excel实体映射类
     * @return
     */
    public static List<Object> readExcel(InputStream is, Class clazz){
        BufferedInputStream bis = null;
        ExcelListener listener = new ExcelListener();
        List<Object> excelEntityList=null;
//        List<Object> excelEntityList1=null;
        try {
            bis = new BufferedInputStream(is);
            // 解析每行结果在listener中处理
            ExcelReader excelReader = EasyExcelFactory.getReader(bis, listener);
            excelReader.read(new Sheet(1, 1, clazz));
            excelEntityList=listener.getDatas();
        } catch (Exception e) {
            e.printStackTrace();
            return excelEntityList;
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return excelEntityList;
    }

    /**
     *
     * @param os 文件输出流
     * @param clazz Excel实体映射类
     * @param data 导出数据
     * @return
     */
    public static Boolean writeExcel(OutputStream os, Class clazz, List<? extends BaseRowModel> data){
        BufferedOutputStream bos= null;
        try {
            bos = new BufferedOutputStream(os);
            ExcelWriter writer = new ExcelWriter(bos, ExcelTypeEnum.XLSX);
            //写第一个sheet, sheet1  数据全是List<String> 无模型映射关系
            Sheet sheet1 = new Sheet(0, 0,clazz);
            writer.write(data, sheet1);
            writer.finish();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return true;
    }

    /**
     * 导出内容
     * @param request
     * @param response
     * @param data   要导出的数据集合
     * @param fileName   导出excel表名字
     * @param clazz      导出实体的字节码文件
     */
    public void cooperationData(HttpServletRequest request, HttpServletResponse response, List<? extends BaseRowModel> data, String fileName, Class clazz) {
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            response.setContentType("multipart/form-data");
            response.setCharacterEncoding("utf-8");

            response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(fileName,"UTF-8")+".xlsx");
            //把数据明细放在list data中
            //System.out.println("把数据明细放在list data中:请完善查询数据接口调用，并把查询结果写入list data中");
            Boolean flag = ExcelUtils.writeExcel(out, clazz, data);
            //System.out.println("导出是否成功："+flag);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

        //2.导出模板Excel
//        FileOutputStream fos = null;
//        try {
//            fos = new FileOutputStream("D:\\export.xlsx");
//            //FileOutputStream fos, Class clazz, List<? extends BaseRowModel> data
//            List<ExcelEntity> list = new ArrayList<>();
//            for (int i = 0; i < 5; i++){
//                ExcelEntity excelEntity = new ExcelEntity();
//                excelEntity.setName("我是名字"+i);
//                list.add(excelEntity);
//            }
//            Boolean flag = ExcelUtils.writeExcel(fos,ExcelEntity.class,list);
//            System.out.println("导出是否成功："+flag);
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        }finally {
//            if (fos != null){
//                try {
//                    fos.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
}
